package com.blb.controller;



import com.blb.entity.Company;
import com.blb.entity.Settlement;
import com.blb.entity.Shipping;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ICompanyService;
import com.blb.service.IProductiveProcessService;
import com.blb.service.ISettlementService;
import com.blb.service.IShippingService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**/
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhengSaiNan
 * @since 2019-11-16
 */
@Controller
@RequestMapping("/shippings")
public class ShippingController {
    @Resource
    private ISettlementService iSettlementService;

    @Resource
    private IShippingService shippingService;
    @Resource
    private ICompanyService companyService;
    @Resource
    private IProductiveProcessService productiveProcessService;

    @RequiresPermissions("shippings")
    @RequestMapping("dd")
    public String gotodd(Model model,String orderNum){
        Map<String, Object> detailMessage = shippingService.getDetailMessage(orderNum);
        model.addAttribute("detailMessage",detailMessage);
        User user = ShiroUtils.getUser();
        model.addAttribute("user",user);
        return "/warehouse/dd_dydd";
    }
    @RequiresPermissions("shippings")
    @RequestMapping("dy")
    public String gotody(Model model,String orderNum){
        Map<String, Object> detailMessage = shippingService.getDetailMessage(orderNum);
        model.addAttribute("detailMessage",detailMessage);
        User user = ShiroUtils.getUser();
        model.addAttribute("user",user);
        return "/warehouse/dd_dyfhd";
    }
    //查询订单列表
    @RequiresPermissions("shippings")
    @GetMapping("shipping")
    public String ddfh(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Map<String, Object>> allLogistics = shippingService.getAllLogistics(companyId);
        Company company = companyService.getById(companyId);
        model.addAttribute("company",company);
        model.addAttribute("allLogistics",allLogistics);

        return "warehouse/dd_fh";
    }

    @RequiresPermissions("shippings")
    @ResponseBody
    @RequestMapping("page")
    public BlbPage page(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        //查询分页数据
        List<Map<String, Object>> allShipMessage = shippingService.getAllShipMessage(companyId);
        //查询条数
        int count = shippingService.countShipMessage(companyId);
        return new DataTablePage(count,count,allShipMessage);
    }

    //查询车辆类型
    @RequiresPermissions("shippings")
    @ResponseBody
    @RequestMapping("allVehicleType")
    public ResponseHelp vehicleType(String logisticsId){
        List<String> allVehicleType = shippingService.getAllVehicleType(logisticsId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allVehicleType);
        return responseHelp;
    }
    //查询司机信息
    @RequiresPermissions("shippings")
    @ResponseBody
    @RequestMapping("allDriverMessage")
    public ResponseHelp driverMessage(String logisticsId,String vehicle_type){
        List<Map<String, Object>> allDriverMessage = shippingService.getAllDriverMessage(logisticsId, vehicle_type);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allDriverMessage);
        return responseHelp;
    }
    //事务处理
    @RequiresPermissions("shippings")
    @ResponseBody
    @RequestMapping("saveShipping")
    @Transactional
    public ResponseHelp addShipping(Shipping shipping){
        shipping.setId(UUID.randomUUID().toString().replace("-",""));
        shipping.setCompanyId(ShiroUtils.getUser().getCompanyId());
        shippingService.updateOrder(shipping);
        productiveProcessService.insertP(shipping.getOrderNum(),3);
        shippingService.save(shipping);
        shippingService.updateVehicle(shipping.getShippingTruckId());
        shippingService.updateDriver(shipping.getDriverId());

        Settlement settlement=new Settlement();
        settlement.setShippingNum(shipping.getShippingNum());
        System.out.println(UUIDUtils.getId());
        settlement.setSettlementId(UUIDUtils.getId());
        settlement.setSettlementStatus("-1");
        System.out.println(settlement);
        iSettlementService.addSettlement(settlement);

        return new ResponseHelp();
    }

    /*//刷新订单列表
    @RequiresPermissions("shippings")
    @GetMapping("refreshShipping")
    public List<Map<String, Object>> refresh(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Map<String, Object>> refreshShipMessage = shippingService.getAllShipMessage(companyId);
        model.addAttribute("refreshShipMessage",refreshShipMessage);
        return refreshShipMessage;
    }*/

}

